﻿using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace 股票监控程序.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private bool isTopMost;

        public bool IsTopMost
        {
            get { return isTopMost; }
            set { isTopMost = value; RaisePropertyChanged(); }
        }

        public ObservableCollection<StockInfo> CList { get; set; }
        public DelegateCommand MinCommand { get; set; }
        public DelegateCommand IsTopMostCommand { get; set; }
        public DelegateCommand CloseCommand { get; set; }

        public MainWindowViewModel()
        {
            if (!File.Exists("c.json"))
            {
                string f = JsonConvert.SerializeObject(new StockInfo[] { new StockInfo()}, Formatting.Indented);
                File.WriteAllText("c.json", f);
                Environment.Exit(0);
            }
            string j = File.ReadAllText("c.json");
            StockInfo[] re = JsonConvert.DeserializeObject<StockInfo[]>(j);
            CList = new ObservableCollection<StockInfo>();

            foreach (var item in re)
            {
                CList.Add(item);
            }

            IsTopMostCommand = new DelegateCommand(() =>
            {
                if (IsTopMost)
                {
                    IsTopMost = false;
                    return;
                }
                IsTopMost = true;
            });

            CloseCommand = new DelegateCommand(() =>
            {
                Environment.Exit(0);
            });
        }
    }
}
